package com.authine.cloudpivot.ext.controller;

import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectQueryModel;
import com.authine.cloudpivot.engine.component.query.api.FilterExpression;
import com.authine.cloudpivot.engine.component.query.api.Page;
import com.authine.cloudpivot.engine.component.query.api.helper.Q;
import com.authine.cloudpivot.engine.enums.ErrCode;
import com.authine.cloudpivot.engine.enums.type.QueryDisplayType;
import com.authine.cloudpivot.ext.domain.TotalCountQueryModel;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import com.google.common.collect.Lists;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author chenjq
 * @date 2021/06/24
 */
@Api(value = "钉办数字驾驶舱云枢接口", tags = "01::EXTAPI::钉办数字驾驶舱云枢接口")
@RestController
@RequestMapping("/ext/nail_digital_cockpit")
// @Slf4j
public class NailDigitalCockpitController extends BaseController {

    @SuppressWarnings("rawtypes")
    @ApiOperation("获取模型数据总量")
    @PostMapping("/get_data_total")
    public ResponseResult getDataTotal(@RequestBody List<TotalCountQueryModel> totalCountQueryModelList) {
        totalCountQueryModelList.forEach(totalCountQueryModel -> {
            // 村部门id
            String corpId = totalCountQueryModel.getCorpId();
            // 镇部门id
            String town = totalCountQueryModel.getTown();
            // 状态
            String sequenceStatus = totalCountQueryModel.getSequenceStatus();
            String schemaCode = totalCountQueryModel.getSchemaCode();
            List<FilterExpression> filterExpressionList = Lists.newArrayList();
            if (StringUtils.isNotEmpty(corpId)) {
                filterExpressionList.add(Q.it("corpId", FilterExpression.Op.Eq, corpId));
            }
            if (StringUtils.isNotEmpty(town)) {
                filterExpressionList.add(Q.it("town", FilterExpression.Op.Like, town));
            }
            if (null != sequenceStatus) {
                filterExpressionList.add(Q.it("sequenceStatus", FilterExpression.Op.Eq, sequenceStatus));
            }
            BizObjectQueryModel bizObjectQueryModel = new BizObjectQueryModel();
            bizObjectQueryModel.setSchemaCode(schemaCode);
            bizObjectQueryModel.setQueryCode(schemaCode);
            if (filterExpressionList.size() == 1) {
                bizObjectQueryModel.setFilterExpr(filterExpressionList.get(0));
            } else if (filterExpressionList.size() > 1) {
                bizObjectQueryModel.setFilterExpr(Q.and(filterExpressionList));
            }
            BizObjectQueryModel.Options options = new BizObjectQueryModel.Options();
            options.setQueryDisplayType(QueryDisplayType.OVERRIDE);
            List<String> columns = Lists.newArrayList();
            columns.add("id");
            options.setCustomDisplayColumns(columns);
            bizObjectQueryModel.setOptions(options);
            Page<BizObjectModel> bizObjectModelPage = getBizObjectFacade().queryBizObjects(bizObjectQueryModel);
            totalCountQueryModel.setTotal(bizObjectModelPage.getTotal());
        });
        return getOkResponseResult(totalCountQueryModelList, ErrCode.OK.getErrMsg());
    }

}
